package com.ruoyi.balanceReturn.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.ruoyi.balanceReturn.domain.HouseReturnT;
import com.ruoyi.balanceReturn.mapper.HouseReturnTMapper;
import com.ruoyi.balanceReturn.service.IHouseReturnTService;
import com.ruoyi.data.DataUtils;
import com.ruoyi.refund.domain.HouseRefundT;
import com.ruoyi.refund.mapper.HouseRefundTMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.door.mapper.HouseDoorTMapper;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 户管理Service业务层处理
 * 
 * @author lijie
 * @date 2023-05-04
 */
@Service
public class HouseReturnTServiceImpl implements IHouseReturnTService
{
    @Autowired
    private HouseReturnTMapper houseReturnTMapper;
    @Autowired
    private HouseDoorTMapper houseDoorTMapper;
    @Autowired
    private HouseRefundTMapper houseRefundTMapper;

    /**
     * 查询户管理
     * 
     * @param doorId 户管理主键
     * @return 户管理
     */
    @Override
    public HouseDoorT selectHouseDoorTByDoorId(Long doorId)
    {
        return houseReturnTMapper.selectHouseDoorTByDoorId(doorId);
    }

    /**
     * 查询户管理列表
     * 
     * @param houseDoorT 户管理
     * @return 户管理
     */
    @Override
    public List<HouseDoorT> selectHouseDoorTList(HouseDoorT houseDoorT)
    {
        return houseReturnTMapper.selectHouseDoorTList(houseDoorT);
    }

    /**
     * 结余退款
     *
     * @param doorId 结余退款
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseReturnTByDoorIds(Long doorId,String userName)
    {

            HouseDoorT houseDoorT = new HouseDoorT();
            HouseReturnT houseReturnT = new HouseReturnT();
            houseDoorT = houseDoorTMapper.selectHouseDoorTByDoorId(doorId);
            HouseRefundT houseRefundTVO = new HouseRefundT();
            houseRefundTVO.setCostSts("3");
            houseRefundTVO.setCostWay("1");
            houseRefundTVO.setCheckSts("0");
            houseRefundTVO.setCostMoney(-DataUtils.sumMoney(houseDoorT.getBalance(), houseDoorT.getRateMoney()));
            int costId1 = houseRefundTMapper.getCostId();
            houseRefundTVO.setSerial(String.valueOf(costId1+1));
            int costId2= Integer.parseInt((String.valueOf(costId1+1)));
            houseRefundTVO.setCreater(userName);
            houseRefundTVO.setHouseId(houseDoorT.getHouseId());
            houseReturnT.setHouseId(houseDoorT.getHouseId());
            houseReturnT.setCreater(userName);
            houseReturnT.setReturnMoney(houseRefundTVO.getCostMoney());
            Date currentDate = new Date();
            houseRefundTVO.setRefundDate(currentDate);
            houseRefundTMapper.insertHouseCostRefundBatchT(houseRefundTVO);
            houseReturnT.setCostId(houseRefundTVO.getCostId());
            houseReturnTMapper.updateHouseDoorBalanceAndRateT(houseDoorT);
            houseReturnTMapper.insertHouseReturnT(houseReturnT);
            return costId2;

    }

    @Override
    public HouseReturnT selectHouseReturnNote(Long doorId) {
        return houseReturnTMapper.selectHouseReturnNote(doorId);
    }
}
